package com.domain.service.impl;

import com.domain.common.Comments;
import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.dao.*;
import com.domain.pojo.*;
import com.domain.service.IndexService;
import com.domain.service.PushMessageService;
import com.domain.service.UsersService;
import com.domain.utils.UUIDUtils;
import com.domain.utils.jiguang.Jdpush;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ReportTypeMapper reportTypeMapper;
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private FollowMapper followMapper;
    @Autowired
    private UsersHelpMapper usersHelpMapper;
    @Autowired
    private PublishMapper publishMapper;

    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private PublishAcceptanceMapper publishAcceptanceMapper;

    @Autowired
    private ContactsMapper contactsMapper;

    @Autowired

    private PushMessageService pushMessageService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private UsersFillterUsersMapper usersFillterUsersMapper;


    /**
     * 获取评论列表
     *
     * @param publishId
     * @return
     */
    @Override
    public Result getCommentPublishList(String publishId, String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(pageSize));
        List<Comment> comments = commentMapper.selectBypublishId(publishId);
        PageInfo pageInfo = new PageInfo(comments);
        return Result.success("操作成功", pageInfo.getList());
    }

    /**
     * 发布评论
     *
     * @param userId    评论人id
     * @param content   评论内容
     * @param publishId 发布id
     * @return
     */
    @Override
    public Result addComment(String userId, String content, String publishId) {
        Publish publishs = publishMapper.selectByPrimaryKey(publishId);
        //自己评论自己
        if (userId.equals(publishs.getUserId())) {
            return Result.publicResult(401, "自己不能评论自己");
        }
        Comment comment = new Comment();
        comment.setId(UUIDUtils.getRandomUUID());
        comment.setCommentTime(new Date());
        comment.setContent(content);
        comment.setCreatTime(new Date());
        comment.setPublishId(publishId);
        comment.setUserId(userId);
        int result = commentMapper.insertSelective(comment);
        if (result > 0) {
            //获取发布人id
            Publish publish = publishMapper.selectByPrimaryKey(publishId);
            //评论成功推送消息至发布人  文案：你的帮忙信息收到一条新的评论
            String pushuuid = UUIDUtils.getRandomUUID();
            int results = Jdpush.sendToAliasList(publish.getUserId(), "你的帮忙信息收到一条新的评论", pushuuid, Const.comment, "评论通知");
            if (results == 1) {
                //添加推送记录
                pushMessageService.addPublishiMessage(publish.getUserId(), userId, "你的帮忙信息收到一条新的评论", "评论通知", publishId, Const.comment, pushuuid);
            }
        }
        return Result.success("操作成功");
    }

    /**
     * 获取举报类型
     *
     * @return
     */
    @Override
    public Result getReportType() {
        List<ReportType> reportTypes = reportTypeMapper.selectList();
        return Result.success("操作成功", reportTypes);
    }

    /**
     * 举报
     *
     * @param userId       举报人id
     * @param reportTypeId 举报类型id
     * @param content      举报内容
     * @param images       举报图片
     * @param publishId    发布帮忙id
     * @return
     */
    @Override
    public Result addReport(String userId, String reportTypeId, String content, String images, String publishId) {
        Report report = new Report();
        report.setId(UUIDUtils.getRandomUUID());
        report.setPublishId(publishId);
        report.setReportTime(new Date());
        report.setCreatTime(new Date());
        report.setReportContent(content);
        report.setReportImage(images);
        report.setUserId(userId);
        report.setReportType(reportTypeId);
        int result = reportMapper.insertSelective(report);
        if (result > 0) {
            //获取推送人id
            Publish publish = publishMapper.selectByPrimaryKey(publishId);
            String pushuuid = UUIDUtils.getRandomUUID();
            //推送举报  文案  您发布的帮忙信息有违规内容
            int results = Jdpush.sendToAliasList(publish.getUserId(), "您发布的帮忙信息有违规内容", pushuuid, Const.report_feedback, "违规提醒");
            if (results == 1) {
                //添加推送记录
                pushMessageService.addSystemMessage(publish.getUserId(), userId, "您发布的帮忙信息有违规内容", "违规提醒", publishId, Const.report_feedback, pushuuid);
            }
        }
        return Result.success();
    }

    /**
     * 添加关注
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Result addFollow(String userId, String publishId) {
        Follow f = new Follow();
        f.setId(UUIDUtils.getRandomUUID());
        f.setCreatTime(new Date());
        f.setFollowTime(new Date());
        f.setFollowState(Comments.FOLLOW_YES);
        f.setPublishId(publishId);
        f.setUserId(userId);
        followMapper.insertSelective(f);
        return Result.success();
    }

    /**
     * 获取我的关注列表
     *
     * @param userId
     * @return
     */
    @Override
    public Result getUsersFollowList(String userId, String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(20));
        List<Map<String, Object>> mapList = followMapper.selectByUserIdsa(userId);
        if (mapList.size() == 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("followId", "");
            map.put("userId", "");
            map.put("uname", "");
            map.put("uNickName", "");
            map.put("uHeadImage", "");
            map.put("publishId", "");
            map.put("pTitle", "");
            map.put("pType", "");
            map.put("isitPaid", "");
            map.put("redPackeCont", "");
            map.put("icon", "");
            mapList.add(map);
        }
        PageInfo pageInfo = new PageInfo(mapList);
        return Result.success("操作成功", pageInfo.getList());
    }

    /**
     * 取消关注
     *
     * @param followId 关注id
     * @return
     */
    @Override
    public Result removeFollow(String followId) {
        followMapper.deleteByPrimaryKey(followId);
        return Result.success();
    }

    /**
     * 帮转
     *
     * @param helpId    帮转人id
     * @param publishId 发布问题id
     * @param userId    发布人id
     * @return
     */
    @Override
    public Result helpPublish(String helpId, String publishId, String userId, String helpType) {
        //判断本平台只能转发一次
        List<UsersHelp> list = usersHelpMapper.selectHelpIdAndPublish(helpId, publishId);
        if (list.size() > 0) {
            return Result.publicResult(402, "人脉圈只能转发一次");
        }
        //增加帮转记录
        UsersHelp help = new UsersHelp();
        help.setId(UUIDUtils.getRandomUUID());
        help.setCreatTime(new Date());
        help.setHelpId(helpId);
        help.setHelpTime(new Date());
        help.setPublishId(publishId);
        help.setUserId(userId);
        help.setHelpType(new Integer(helpType));
        int result = usersHelpMapper.insertSelective(help);
        if (result > 0) {
            //帮转成功增加发布问题得转发数量
            publishMapper.updateForwardCount(publishId);
            //帮转成功以后推送发布人通知（不做推送  增加消息记录）文案：xxx 帮转了您发布的帮忙请求
            Users user = usersMapper.selectByPrimaryKey(helpId);
            pushMessageService.addPublishiMessage(userId, helpId, "" + user.getName() + "帮转了您发布的帮忙请求", "帮转通知", publishId, Const.help, UUIDUtils.getRandomUUID());
        }
        return Result.success();
    }


    /**
     * 发布受理
     *
     * @param helpId            帮转人id
     * @param acceptanceId      受理人id
     * @param publishId         发布问题id
     * @param userId            发布人id
     * @param acceptanceMessage 受理留言
     * @return
     */
    @Override
    public Result publishAcceptance(String acceptanceId, String publishId, String userId, String acceptanceMessage, String helpId) {
        /*添加受理记录*/
        PublishAcceptance p = new PublishAcceptance();
        String uuid = UUIDUtils.getRandomUUID();
        p.setId(uuid);
        p.setAcceptanceId(acceptanceId);
        p.setAcceptanceMessage(acceptanceMessage);
        p.setAcceptanceTime(new Date());
        p.setCreatTime(new Date());
        p.setPublishId(publishId);
        p.setSpareOne(helpId);
        p.setUserId(userId);
        int result = publishAcceptanceMapper.insertSelective(p);
        if (result > 0) {
            //帮忙受理通知 xxx 接受了您得帮忙，赶快发起讨论组
            Users user = usersMapper.selectByPrimaryKey(acceptanceId);
            String pushId = UUIDUtils.getRandomUUID();
            int results = Jdpush.sendToAliasList(userId, "" + user.getName() + "接受了您得帮忙，赶快发起讨论组", pushId, Const.notice_of_action, "受理通知");
            //如果推送成功添加推送记录
            if (results == 1) {
                //受理通知  spare_one 添加的是受理记录id
                pushMessageService.addPublishiMessage(userId, acceptanceId, "" + user.getName() + "接受了您得帮忙，赶快发起讨论组", "受理通知", uuid, Const.notice_of_action, pushId);
            }

        }
        return Result.success();
    }

    /**
     * 获取首页好友信息
     *
     * @param usersId
     * @return
     */
    @Override
    public Result getIndexUsersInfo(String usersId, String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(20));
        Map<String, Object> maps = new HashMap<>();
        //获取我的好友id
        List<Map<String, Object>> contacts = contactsMapper.selectFrindesId(usersId);
        //获取我通讯录没有过滤我的好友
        for (Map<String, Object> c : contacts
                ) {
            List<UsersFillterUsers> usersFillterUsers = usersFillterUsersMapper.selectByuserIds(c.get("fridesnId").toString(), usersId);
        }

    /*    List<Users> usersList = new ArrayList<>();
        Users user = new Users();
        for (Map<String, Object> map : contacts
                ) {
            //判断我的好友有没有发布
            List<Publish> publishes = publishMapper.selectPublishByUserId(map.get("fridesnId").toString());
            //判断我的好友有没有转发
            user = usersMapper.selectByPrimaryKey(map.get("fridesnId").toString());
            //获取用户好友得好友
            List<Contacts> contactsList = contactsMapper.selectByUsersIdAndType(map.get("fridesnId").toString());
            for (Contacts cs : contactsList
                    ) {
                user = usersMapper.selectByPrimaryKey(cs.getSpareTwo());
                usersList.add(user);
            }
            usersList.add(user);
        }
        PageInfo pageInfo = new PageInfo(usersList);
        maps.put("usersList", pageInfo.getList());*/
        return Result.success("操作成功", maps);
    }

    /**
     * 搜索
     *
     * @param publishType
     * @param city
     * @param gender
     * @param itisPaid
     * @return
     */
    @Override
    public Result serch(String usersId, String publishType, String city, String gender, String itisPaid) {
        //todo
        return null;
    }
}
